﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfFeeCalculator.Model;
using WpfFeeCalculator.ViewModel;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Forms;
using WpfFeeCalculator.Helpers;

namespace WpfFeeCalculator.View
{
    /// <summary>
    /// Interaction logic for CalculationView.xaml
    /// </summary>
    public partial class CalculationView : System.Windows.Controls.UserControl, ICalculationView
    {
        private WorkbookView calculationWorkbookView;
        public CalculationView()
        {
            InitializeComponent();

            if (DataContext != null)
                (DataContext as CalculationViewModel).View = this as ICalculationView;

            calculationWorkbookView = wfHost.Child as WorkbookView;

        }

        public void NotifyUser(string message)
        {
            MessageBox.Show(message);
        }

        public LockWorkbookView LockWorkBook()
        {
            return new LockWorkbookView(calculationWorkbookView);
        }

        public void SetWorkbook(IWorkbook workbook)
        {
            using (LockWorkBook())
            {
                calculationWorkbookView.ActiveWorkbook = workbook;
            }
        }

        public override void EndInit()
        {
            base.EndInit();
            if (DataContext != null)
                (DataContext as CalculationViewModel).View = this as ICalculationView;
        }

        public void CloseTab()
        {

        }

        public void SetInfo(int row, int column, string message, string workSheetName)
        {
            var findCell = calculationWorkbookView.ActiveWorkbook.Worksheets[workSheetName].Cells[row, column];

            findCell.Borders.Color = System.Drawing.Color.Tomato;

            if (findCell.Comment == null)
            {
                IComment comment = findCell.AddComment(message);

                // Turn off the default bold font.
                comment.Shape.TextFrame.Characters.Font.Bold = false;
            }
        }

        private void ComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            PortfolioTypeCmb.SelectedIndex = 0;
        }

        private void PriceCutOffComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            priceCutOffCmb.SelectedIndex = 0;
        }

        public void DoubleClick()
        {
            var b = InformationDG.CurrentCell.Column;
            var row = ((CalculationViewChanges)InformationDG.CurrentItem).Row;
            var column = ((CalculationViewChanges)InformationDG.CurrentItem).Column;
            using (LockWorkBook())
            {
                SetInfo(row, column, "This is the real message", "Calculations");
            }
        }
        
    }
}
